﻿#include "GLLInc.h"
#include "GLLObj.h"

namespace GLL
{
	Object::Object()
	{
		_mgr = 0;
		_id = 0;
	}

	String Object::ID2String(ID t)
	{
		StringStream ss;
		switch(t.type)
		{
		case OT_Light:
			ss << "Lit.";
			break;
		case OT_Camera:
			ss << "Cam.";
			break;
		case OT_Particle:
			ss << "Prtcl.";
			break;
		case OT_Mesh:
			ss << "Msh.";
			break;
		case OT_Helper:
			ss << "Hlpr.";
			break;
		default:
			ss << "NA.";
			break;
		}
		ss << t.index;
		String ret(ss.str());
		return ret;
	}

	Object::~Object()
	{
		ClearAllComs();		
	}

	void Object::Update( TimeMS ms)
	{
		if(OnUpdate(ms))
		{
			UpdateAllComs(ms);
		}
	}

	Size3Df Object::GetScale()
	{
		return Size3Df(1.0f);
	}

	Axis3Df Object::GetRotation()
	{
		return Axis3Df::ZERO();
	}

	void Object::_PostLoad()
	{
		NotifyAllComs();
	}

}

//-----------------------------------------------------------------------------
#include "GLLObjCom.h"

namespace GLL
{

	void Object::AddCom( ObjectComponent* p)
	{
		p->SetObject(this);
		_coms[p->_name] = p;
	}


	ObjectComponent* Object::GetCom( const String& name )
	{
		ComVec::iterator iter = _coms.find(name);
		if(iter == _coms.end()){
			SS ss;ss << "no such animator(" << name << ") in object : "<<StrID();
			GLL_Throw(ss);
			return nullptr;
		}
		return iter->second;
	}

	void Object::RemoveCom( const String& name )
	{
		ComVec::iterator iter = _coms.find(name);
		if(iter == _coms.end()) return;
		GLL_DEL(iter->second);
		_coms.erase(iter);		
	}

	void Object::ClearAllComs()
	{
		ComVec::iterator iter = _coms.begin();
		for(; iter != _coms.end(); ++iter)
		{
			GLL_DEL(iter->second);
		}
		_coms.clear();
	}

	void Object::UpdateAllComs( TimeMS ms)
	{
		ComVec::iterator iter = _coms.begin();
		for(; iter != _coms.end(); ++iter)
		{
			iter->second->Update(ms);
		}
	}

	void Object::NotifyAllComs()
	{
		ComVec::iterator iter = _coms.begin();
		for(; iter != _coms.end(); ++iter)
		{
			iter->second->ObjectLoaded();
		}
	}

}//GLL
